package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.dao.IReportFormDao;
import com.boss.salary.service.IReportFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportFormServiceImpl implements IReportFormService {

    @Autowired
    private IReportFormDao reportFormDao;

    @Override
    public List<Map<String, Object>> salarySign() {
        return reportFormDao.salarySign();
    }

    @Override
    public PageDTO<Map<String, Object>> getUnitSalarySummaryPage(PaginationDTO paginationDTO, String salaryMonth, String salaCardBank) {
        List<Map<String, Object>> unitSalarySummarys = reportFormDao.queryUnitSalarySummaryPage(paginationDTO, salaryMonth, salaCardBank);
        int total = reportFormDao.queryUnitSalarySummaryTotal(salaryMonth, salaCardBank);
        if(!CollectionUtils.isEmpty(unitSalarySummarys)){
            Map<String, Object> unitSalarySummary = reportFormDao.queryUnitSalarySummarySum(salaryMonth, salaCardBank);
            unitSalarySummary.put("单位名称","总计");
            unitSalarySummarys.add(0, unitSalarySummary);
        }
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(unitSalarySummarys);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    public PageDTO<Map<String, Object>> queryUnitBankRetiredPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        // 分页数据
        List<Map<String, Object>> unitBankRetiredPage = reportFormDao.queryUnitBankRetiredPage(paginationDTO, queryParam);
        int total = 0;
        List<String> tableHeads = new ArrayList<>();
        if (!StringUtils.isEmpty(unitBankRetiredPage) && unitBankRetiredPage.size() > 0) {
            tableHeads  = unitBankRetiredPage.get(0).keySet().stream().filter(set ->
                    !set.equals("agency_id") && !set.equals("agency_code") && !set.equals("sala_card_bank") && !set.equals("salary_month")
            ).collect(Collectors.toList());
            // 查询合计值
            Map<String, Object> unitBankRetiredSum = reportFormDao.queryUnitBankRetiredSum(queryParam);
            if (!StringUtils.isEmpty(unitBankRetiredPage) && unitBankRetiredPage.size() > 0) {
                unitBankRetiredSum.put("单位名称","总计");
                unitBankRetiredPage.add(0, unitBankRetiredSum);
            }
            total = reportFormDao.queryUnitBankRetiredTotal(queryParam);
        }
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(unitBankRetiredPage);
        PageDTO.setTotal(total);
        PageDTO.setTableHeads(tableHeads);
        return PageDTO;
    }

    @Override
    public PageDTO<Map<String, Object>> queryUnitBankWorkPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        // 分页数据
        List<Map<String, Object>> unitBankWorkPage = reportFormDao.queryUnitBankWorkPage(paginationDTO, queryParam);
        int total = 0;
        List<String> tableHeads = new ArrayList<>();
        if (!StringUtils.isEmpty(unitBankWorkPage) && unitBankWorkPage.size() > 0) {
            // table表头
            tableHeads  = unitBankWorkPage.get(0).keySet().stream().filter(set ->
                    !set.equals("agency_id") && !set.equals("agency_code") && !set.equals("sala_card_bank") && !set.equals("salary_month")
            ).collect(Collectors.toList());
            // 查询合计值
            Map<String, Object> unitBankWorkSum = reportFormDao.queryUnitBankWorkSum(queryParam);
            if (!StringUtils.isEmpty(unitBankWorkPage) && unitBankWorkPage.size() > 0) {
                unitBankWorkSum.put("单位名称","总计");
                unitBankWorkPage.add(0, unitBankWorkSum);
            }
            // 查询total数量
            total = reportFormDao.queryUnitBankWorkTotal(queryParam);
        }
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(unitBankWorkPage);
        PageDTO.setTotal(total);
        PageDTO.setTableHeads(tableHeads);
        return PageDTO;
    }




    @Override
    public PageDTO<Map<String, Object>> vwSalPersonChangeAdd(PaginationDTO paginationDTO, String salaryMonth) {
        List<Map<String, Object>> SalPersonChangeAdd = reportFormDao.vwSalPersonChangeAdd(paginationDTO, salaryMonth);
        int total = reportFormDao.vwSalPersonChangeAddTotal(salaryMonth);
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(SalPersonChangeAdd);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    public PageDTO<Map<String, Object>> vwSalPersonChangeDelete(PaginationDTO paginationDTO, String salaryMonth) {
        List<Map<String, Object>> SalPersonChangeAdd = reportFormDao.vwSalPersonChangeDelete(paginationDTO, salaryMonth);
        int total = reportFormDao.vwSalPersonChangeDeleteTotal(salaryMonth);
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(SalPersonChangeAdd);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    public PageDTO<Map<String, Object>> queryPersonChangeTurnPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {

        List<Map<String, Object>> personChangeTurn = reportFormDao.queryPersonChangeTurnPage(paginationDTO, queryParam);
        int total = reportFormDao.getPersonChangeTurnNum(queryParam);
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(personChangeTurn);
        PageDTO.setTotal(total);
        return PageDTO;
    }


    @Override
    public PageDTO<Map<String, Object>> vwSalAgencyPersonTrafficAllowance(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        // 分页数据
        List<Map<String, Object>> trafficAllowance = reportFormDao.vwSalAgencyPersonTrafficAllowance(paginationDTO, queryParam);
        int total = 0;
        List<String> tableHeads = new ArrayList<>();
        if (!StringUtils.isEmpty(trafficAllowance) && trafficAllowance.size() > 0) {
            // table表头
            tableHeads  = trafficAllowance.get(0).keySet().stream().filter(set ->
                    !set.equals("agency_id") && !set.equals("agency_code") && !set.equals("sala_card_bank") && !set.equals("salary_month")
            ).collect(Collectors.toList());
            // 查询合计值
            Map<String, Object> unitBankWorkSum = reportFormDao.trafficAllowanceSum(queryParam);
            if (!StringUtils.isEmpty(trafficAllowance) && trafficAllowance.size() > 0) {
                unitBankWorkSum.put("单位名称","总计");
                trafficAllowance.add(0, unitBankWorkSum);
            }
            // 查询total数量
            total = reportFormDao.trafficAllowanceTotal(queryParam);
        }
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(trafficAllowance);
        PageDTO.setTotal(total);
        PageDTO.setTableHeads(tableHeads);
        return PageDTO;
    }
}
